package com.hao_family.atcrowdfunding.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hao_family.atcrowdfunding.beans.TAdmin;
import com.hao_family.atcrowdfunding.beans.TAdminExample;
import com.hao_family.atcrowdfunding.beans.TAdminExample.Criteria;
import com.hao_family.atcrowdfunding.beans.TMenu;
import com.hao_family.atcrowdfunding.beans.TMenuExample;
import com.hao_family.atcrowdfunding.constant.AppConstant;
import com.hao_family.atcrowdfunding.mapper.TAdminMapper;
import com.hao_family.atcrowdfunding.mapper.TMenuMapper;
import com.hao_family.atcrowdfunding.service.AdminService;
import com.hao_family.atcrowdfunding.utils.AppUtils;

@Service
public class AdminServiceImpl implements AdminService {
	@Autowired
	TAdminMapper adminMapper;

	@Autowired
	TMenuMapper menuMapper;

	@Override
	public TAdmin getAdminByUsernameAndPassword(String username, String password) {
		// TODO Auto-generated method stub
		String pwd = AppUtils.getDigestPsd(password);
		TAdminExample example = new TAdminExample();
		Criteria criteria = example.createCriteria();
		criteria.andLoginacctEqualTo(username).andUserpswdEqualTo(pwd);
		List<TAdmin> list = adminMapper.selectByExample(example);
		return list != null && list.size() == 1 ? list.get(0) : null;
	}

	@Override
	public List<TMenu> listMenus() {
		// TODO Auto-generated method stub
		TMenuExample example = new TMenuExample();
		example.createCriteria().andPidEqualTo(0);
		List<TMenu> list = menuMapper.selectByExample(example);
		for (TMenu tMenu : list) {
			TMenuExample example2 = new TMenuExample();
			example2.createCriteria().andPidEqualTo(tMenu.getId());
			tMenu.setChilds(menuMapper.selectByExample(example2));
		}
		return list;
	}

	@Override
	public List<TAdmin> getAdminList(String condition) {
		// TODO Auto-generated method stub
		TAdminExample example = new TAdminExample();
		if (!condition.isEmpty()) {
			Criteria crt0 = example.createCriteria();
			crt0.andLoginacctLike("%" + condition + "%");

			Criteria crt1 = example.createCriteria();
			crt0.andUsernameLike("%" + condition + "%");

			Criteria crt2 = example.createCriteria();
			crt0.andEmailLike("%" + condition + "%");
			example.or(crt1);
			example.or(crt2);
		}

		List<TAdmin> list = adminMapper.selectByExample(example);
		return list;
	}

	/**
	 * 依据id返回admin对象
	 */
	@Override
	public TAdmin getAdminById(Integer id) {
		// TODO Auto-generated method stub
		return adminMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean updateAdmin(TAdmin admin) {
		// TODO Auto-generated method stub
		int i = adminMapper.updateByPrimaryKeySelective(admin);
		return i == 1 ? true : false;
	}

	@Override
	public String saveAdmin(TAdmin admin) {
		// TODO Auto-generated method stub
		// 修改成功默认返回消息为空
		String msg = null;
		boolean a = checkEmail(admin.getEmail());
		boolean b = checkLoginacct(admin.getLoginacct());
		if (a & b) {
			// 添加初始密码并加密
			admin.setUserpswd(AppUtils.getDigestPsd(AppConstant.DEFAULT_ADMIN_PASSWORD));
			adminMapper.insertSelective(admin);
		} else if (a & !b) {
			// 状态是，帐号占用
			msg = "帐号以被占用！";
		} else if (!a & b) {
			// 状态是，邮箱占用
			msg = "邮箱以被占用！";
		} else {
			// 状态是，邮箱帐号均被占用
			msg = "帐号邮箱以被占用！";
		}
		return msg;
	}

	@Override
	public boolean checkLoginacct(String loginacct) {
		// TODO Auto-generated method stub
		TAdminExample example = new TAdminExample();
		Criteria criteria = example.createCriteria().andLoginacctEqualTo(loginacct);
		return adminMapper.selectByExample(example).isEmpty();
	}

	@Override
	public boolean checkEmail(String email) {
		// TODO Auto-generated method stub
		TAdminExample example = new TAdminExample();
		Criteria criteria = example.createCriteria().andEmailEqualTo(email);
		return adminMapper.selectByExample(example).isEmpty();
	}

	@Override
	public boolean deleteAdminById(Integer id) {
		// TODO Auto-generated method stub
		int i = adminMapper.deleteByPrimaryKey(id);
		return i == 1 ? true : false;
	}

}
